package vistas2;

import Dominio.Controladores.CtrlConsultaTemas;
import Dominio.Controladores.CtrlConsultas;
import Dominio.CtrlDominio;
import Dominio.NoEstaTemaException;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  kaolla86
 */
public class ConsultarTema extends javax.swing.JFrame {

    private menu menu;
    private CtrlConsultaTemas CD = new CtrlConsultaTemas();
    private Vector noticias = new Vector<String>(0, 1);
    private Vector temas = new Vector<String>();
    private static String[] cabecerasTablaTema = {"Temas"};
    private static String[] cabecerasTablaNoticia = {"Noticias del tema"};

    /** Creates new form ConsultarTema */
    public ConsultarTema(menu m) {
        //   try {
            /*String pt = "/home/sandra/Escritorio/diccTema.dat";
        String pn = "/home/sandra/Escritorio/diccNot.dat";
        String pp = "/home/sandra/Escritorio/diccPal.dat";
        String pathPalNoFunc = "xe";
        CD.cargarDiccionarios(pn, pp, pt, pathPalNoFunc);*/
        menu = m;
        temas = CD.listarTemas();
        if (temas.size() == 0) {
            JOptionPane.showMessageDialog(this, "No es posible consultar los temas, porque no hay ning\u00fan tema en el diccionario", "No hay temas en el diccionario", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
            menu.setVisible(true);
        } else {
            this.setTitle("Consultar Temas");
            initComponents();
            this.setLocationRelativeTo(null);
            Object[][] aux = new Object[temas.size()][1];
            for (int i = 0; i < temas.size(); i++) {
                aux[i][0] = temas.get(i);
            }
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaTema));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTemas = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jNoticias = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jConsNot = new javax.swing.JButton();
        jSalir = new javax.swing.JButton();

        jButton7.setText("Ok");

        jButton8.setText("Consultar Noticia");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Salir");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton9)
                            .addComponent(jButton8))))
                .addContainerGap(91, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jButton7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton9))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTemas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Temas"
            }
        ));
        jTemas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTemasMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTemas);

        jNoticias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Noticias del Tema"
            }
        ));
        jScrollPane3.setViewportView(jNoticias);

        jConsNot.setText("Consultar la Noticia");
        jConsNot.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jConsNotMouseClicked(evt);
            }
        });
        jConsNot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jConsNotActionPerformed(evt);
            }
        });

        jSalir.setText("Salir");
        jSalir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jSalirMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jConsNot, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jConsNot)
                .addGap(30, 30, 30)
                .addComponent(jSalir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jConsNotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jConsNotActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jConsNotActionPerformed

    private void jSalirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSalirMouseClicked
        this.dispose();
        menu.setVisible(true);
    }//GEN-LAST:event_jSalirMouseClicked

    private void jConsNotMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jConsNotMouseClicked
        int fila = jNoticias.getSelectedRow();
        if (fila == -1) {
            JOptionPane.showMessageDialog(this, "Selecciona una noticia para consultar", "No se ha seleccionado una noticia", JOptionPane.INFORMATION_MESSAGE);
        } else {
            String noticia = (String) jNoticias.getValueAt(fila, 0);
            if (noticia == null) {
                JOptionPane.showMessageDialog(this, "No hay ninguna noticia Seleccionada. Seleccione un Tema y una noticia, y vuelva a probar", "No se ha seleccionado ning\u00fana noticia.", JOptionPane.INFORMATION_MESSAGE);
            } else {
                this.dispose();
                new ConsNoticias(noticia, this).setVisible(true);
            }
        }        
    }//GEN-LAST:event_jConsNotMouseClicked

    private void jTemasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTemasMouseClicked
        int fila = jTemas.getSelectedRow();
        if (fila == -1) {
            JOptionPane.showMessageDialog(this, "Selecciona un tema para consultar", "No se ha seleccionado un tema", JOptionPane.INFORMATION_MESSAGE);
        } else {
            noticias = CD.listarNoticasTema(fila);
            if (noticias.size() == 0) {
                JOptionPane.showMessageDialog(this, "El tema que deseas consultar no tiene noticias asociadas a \u00e9l", "El tema no tiene noticias", JOptionPane.INFORMATION_MESSAGE);
            } else {
                Object[][] aux = new Object[noticias.size()][1];
                for (int i = 0; i < noticias.size(); i++) {
                    aux[i][0] = noticias.get(i);
                }
                jNoticias.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaNoticia));
            }
        }
    }//GEN-LAST:event_jTemasMouseClicked
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ConsultarTema(idioma).setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JButton jConsNot;
    private javax.swing.JTable jNoticias;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JButton jSalir;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTemas;
    // End of variables declaration//GEN-END:variables
}
